<?php

include __DIR__."/functions.php";
include __DIR__."/vendor/autoload.php";

class AccessDataTask
{
    protected static $medooConfig = [
        'database_type' => 'mysql',
        'database_name' => 'bus84',
        'server' => 'localhost',
        'username' => 'root',
        'password' => '123456',
        'charset' => 'utf8'
    ];
    protected static $appKey = "Yv9cL8wTwZgr";
    protected static $container = [];

    public static function getInstance($key,$params)
    {
        if(empty(self::$container[$key]))
        {
            self::$container[$key] = new $key($params);
        }
        return self::$container[$key];
    }
    public static function getCityAndSFInfo()
    {
        $url = "http://js.8684.cn/citys/city_boxInf.min.js";
        $content = curlRequest($url);
        $cityPinYinMatchResult = preg_match("/citypyjson\=(\{.*?\}\})/m",$content,$cityPinYinMatches);
        $citySFMatchResult = preg_match("/citysfjson\=(\{.*?\}\})/m",$content,$citySFMatches);

        if($cityPinYinMatchResult && $citySFMatchResult){
            $cityPinYinInfo = preg_replace("/(\w+):/m",'"$1":',$cityPinYinMatches[1]);
            $citySFInfo = preg_replace("/(\w+):/m",'"$1":',$citySFMatches[1]);

            $content = ['pinyin'=>json_decode($cityPinYinInfo,true),'sf'=>json_decode($citySFInfo,true)];

            return $content;
        }
        return false;
    }
    public function addCityInfo($cityInfo)
    {
        $medooInstance =  AccessDataTask::getInstance('medoo',self::$medooConfig);

        foreach($cityInfo as $first_letter=>$cities)
        {
            foreach($cities as $cityPinYin=>$cityCNName)
            {
                if(!$medooInstance->has('city',['pinyin'=>$cityPinYin])){
                    $medooInstance->insert('city',['pinyin'=>$cityPinYin,'cn_name'=>$cityCNName,'first_letter'=>$first_letter]);
                }
            }
        }
    }
    public function addSFInfo($SFInfo)
    {
        $medooInstance =  AccessDataTask::getInstance('medoo',self::$medooConfig);

        foreach($SFInfo as $sf=>$cities)
        {
            foreach($cities as $pinyin =>$cityCNName)
            {
                if($medooInstance->has('city',['pinyin'=>$pinyin])){
                    $medooInstance->update('city',['province_cn_name'=>$sf],['pinyin'=>$pinyin]);
                }
            }
        }
    }
    public  static  function getLinesByCity($pinyin)
    {
        $url = "http://api.8684.cn/bus_api_v1.php";
        $appKey = self::$appKey;
        $postContent = "appkey=$appKey&k=all_lines&ecity=$pinyin";
        $jsonResult = curlRequest($url,$postContent,"POST");

        $jsonArray =  json_decode($jsonResult,true);

        return isset($jsonArray['data']) ? $jsonArray['data'] : [];
    }

    public  function addLines($cityPinyin)
    {
        $medooInstance =  AccessDataTask::getInstance('medoo',self::$medooConfig);

        $linesInfo = self::getLinesByCity($cityPinyin);
        foreach($linesInfo as $k=>$lines){
            $inlineLines = $lines['lines'];
            foreach($inlineLines as $line)
            {
                $line['type'] = $lines['type'];
                $line['city_pinyin'] = $cityPinyin;
                if(!$medooInstance->has('line',['busw'=>$line['busw']]))
                {
                    $medooInstance->insert('line',$line);
                }
            }
        }
    }
    public static function getLineInfo($city,$code)
    {
        $url = "http://api.8684.cn/bus_api_v1.php";
        $appKey = self::$appKey;
        $postContent = "k=the_line&appkey=$appKey&ecity=$city&q=$code";
        $jsonContent = curlRequest($url,$postContent,'POST');
        $jsonArray = json_decode($jsonContent,true);
        return isset($jsonArray['data']) ? $jsonArray['data'] : [];
    }
    public function addLineInfo($city,$code)
    {
        $medooInstance =  AccessDataTask::getInstance('medoo',self::$medooConfig);
        //完善line表
        $lineInfo = self::getLineInfo($city,$code);

        //完善line_info表

    }
    public function run()
    {
        /* 获取city表数据 */
        $cityInfo = AccessDataTask::getCityAndSFInfo();
        $pinyin = $cityInfo['pinyin'];
        $sf = $cityInfo['sf'];

        $this->addCityInfo($pinyin);
        $this->addSFInfo($sf);

        /* 获取line表数据 */
        foreach($pinyin as $sf=>$cities){
            foreach($cities as $pinyin =>$cityCNName)
            {
                $this->addLines($pinyin);
            }
        }
        /* 获取station表数据*/

    }
}

(new AccessDataTask())->run();
